<?php
/**
 * Provides methods to interact with participants
*
* @author Badr Mehdaoui
* @name participant
* @version 0.7
* @package Collabtive
* @link http://www.o-dyn.de
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or laterg
*/
class participant {
	public $mylog;
	
	/**
	 * Constructor
	 * Initializes event log
	 */
	function __construct()
	{
		$this->mylog = new mylog;
	}
	
	/**
	 * Creates a participant
	 *
	 * @param string $type Type of the participant
	 * @param string $company Company of the participant
	 * @param string $contactPerson Contact Person of the participant
	 * @param string $mail Mail of the participant
	 * @param string $phone Phone of the participant
	 * @param string $cellPhone CellPhone of the participant
	 * @param string $url URL of the participant
	 * @param string $address Address of the participant
	 * @param string $postcode Postcode of the participant
	 * @param string $city City of the participant
	 * @param string $country Country of the participant
	 * @param string $state State of the participant
	 * @param string $description Description of the participant
	 * @param boolean $isDeleted DeletedParticipant
	 * @return int $insid ID of the newly created participant
	 */
	function add($name,$email,$type,$phone,$cellphone,$company,$contactPerson,$url,$address,$city,$country,$postcode,$description)
	{
		global $conn;
	
		$query = $conn->prepare(
				"INSERT INTO participant 
				(name,email,type,phone,cellphone,company,contactPerson,url,address,city,country,postcode,description)
				VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
				);
		$result = $query->execute(array($name,$email,$type,$phone,$cellphone,$company,$contactPerson,$url,$address,$city,$country,$postcode,$description));
	
		if ($result) {
			$insid = $conn->lastInsertId();
			$this->mylog->add($email, 'participant', 1, 0);
			return $insid;
		} else {
			return false;
		}
	}
	
	function deleteParticipant($id)
	{	global $conn;
		$userid = $_SESSION["userid"];
		$id = (int) $id;
		$query = $conn->prepare("UPDATE `participant` set `isDeleted` = 1 where ID = $id");
		$result = $query->execute();
		if ($result) {
			$this->mylog->add($userid, 'delParticipant', 3, $id);
			return true;
		} else {
			return false;
		}
	}

	function updateParticipant($id, $name, $email, $type, $phone, $cellphone, $company, $contactPerson, 
 			$url, $address, $city, $state, $country, $postcode, $description)
	{	global $conn;
		$userid = $_SESSION["userid"];
		$id = (int) $id;
		$type = (int) $type;
		$query = $conn->prepare("UPDATE `participant` SET `name`= '$name', `email`= '$email', `type`= '$type', `phone`= '$phone', 
				`cellphone`= '$cellphone', `company`= '$company', `contactPerson`= '$contactPerson', `url`= '$url', 
				`address`= '$address', `city`= '$city', `state`= '$state', `country`= '$country', `postcode`= '$postcode',
				 `description`= '$description' WHERE ID = $id");
		$result = $query->execute();
		
		if ($result) {
			$this->mylog->add($userid, 'updateParticipant', 3, $id);
			return true;
		} else {
			return false;
		}
	}
	
	function searchParticipants($keyword) {
		global $conn;
		
		if (strlen($keyword)!==0){
			$request = "SELECT * FROM participant JOIN type ON type.type_ID = participant.type WHERE isDeleted != 1";
			$sel = $conn->query($request);
			$participants = array();
			
			while ($participant = $sel->fetch()) {
				if ($this->is_str_contain(strtolower($participant["name"]), strtolower($keyword)) 
					or $this->is_str_contain(strtolower($participant["email"]), strtolower($keyword)) 
					or $this->is_str_contain(strtolower($participant["phone"]), strtolower($keyword))	
					or $this->is_str_contain(strtolower($participant["company"]), strtolower($keyword))	
					or $this->is_str_contain(strtolower($participant["city"]), strtolower($keyword))
					or $this->is_str_contain(strtolower($participant["state"]), strtolower($keyword))	
					or $this->is_str_contain(strtolower($participant["country"]), strtolower($keyword))
					or $this->is_str_contain(strtolower($participant["type_name"]), strtolower($keyword))	){
					array_push($participants, $participant);
				}
			}
			
			if (!empty($participants)) {
				return $participants;
			}else {
				return false;
			}
			
		} else {
			return false;
		}
			
	
		
	}
	
	function getTypeName($typeId)
	{
		global $conn;
		$typeId = (int) $typeId;
		$sel = $conn->query("SELECT type_name FROM type WHERE type_ID = $typeId");
		$typeName = $sel->fetch();
		$result = $typeName["type_name"];
		return $result;
	}
	
	function getProfile($id)
	{
		global $conn;
		$id = (int) $id;
	
		$sel = $conn->query("SELECT * FROM participant JOIN type ON type.type_ID = participant.type WHERE isDeleted != 1 AND ID = $id");
		$profile = $sel->fetch();
		if (!empty($profile)) {
			$profile["name"] = stripslashes($profile["name"]);
			if (isset($profile["company"])) {
				$profile["company"] = stripslashes($profile["company"]);
			}
			if (isset($profile["adress"])) {
				$profile["adress"] = stripslashes($profile["adress"]);
			}
			if (isset($profile["adress2"])) {
				$profile["adress2"] = stripslashes($profile["adress2"]);
			}
			if (isset($profile["state"])) {
				$profile["state"] = stripslashes($profile["state"]);
			}
			if (isset($profile["country"])) {
				$profile["country"] = stripslashes($profile["country"]);
			}
			if (isset($profile["type_name"])) {
				$profile["type_name"] = stripslashes($profile["type_name"]);
			}

	
			return $profile;
		} else {
			return false;
		}
	}
	
	function getAllParticipants()
	{
		global $conn;
	
		$sel = $conn->query("SELECT  * FROM participant  JOIN type ON type.type_ID = participant.type WHERE isDeleted != 1");
		$participants = array();
	
		while ($participant = $sel->fetch()) {
			array_push($participants, $participant);
		}
	
		if (!empty($participants)) {
			return $participants;
		}else {
			return false;
		}
	}
	
	function getParticipants($lim = 10)
	{
		global $conn;
	
		$lim = (int) $lim;
	
		$num = $conn->query("SELECT COUNT(*) FROM `participant` WHERE isDeleted != 1")->fetch();
		$num = $num[0];
		SmartyPaginate::connect();
		// set items per page
		SmartyPaginate::setLimit($lim);
		SmartyPaginate::setTotal($num);
	
		$start = SmartyPaginate::getCurrentIndex();
		$lim = SmartyPaginate::getLimit();
	
		$sel2 = $conn->query("SELECT ID FROM `participant` WHERE isDeleted != 1 ORDER BY ID ASC LIMIT $start,$lim");
	
		$participants = array();
		while ($participant = $sel2->fetch()) {
			array_push($participants, $this->getProfile($participant["ID"]));
		}
	
		if (!empty($participants)) {
			return $participants;
		} else {
			return false;
		}
	}
	
	function is_str_contain($string, $keyword)
	{
		if (empty($string) || empty($keyword)) return false;
		$keyword_first_char = $keyword[0];
		$keyword_length = strlen($keyword);
		$string_length = strlen($string);
	
		// case 1
		if ($string_length < $keyword_length) return false;
	
		// case 2
		if ($string_length == $keyword_length) {
			if ($string == $keyword) return true;
			else return false;
		}
	
		// case 3
		if ($keyword_length == 1) {
			for ($i = 0; $i < $string_length; $i++) {
	
				// Check if keyword's first char == string's first char
				if ($keyword_first_char == $string[$i]) {
					return true;
				}
			}
		}
	
		// case 4
		if ($keyword_length > 1) {
			for ($i = 0; $i < $string_length; $i++) {
				/*
				 the remaining part of the string is equal or greater than the keyword
				 */
				if (($string_length + 1 - $i) >= $keyword_length) {
	
					// Check if keyword's first char == string's first char
					if ($keyword_first_char == $string[$i]) {
						$match = 1;
						for ($j = 1; $j < $keyword_length; $j++) {
							if (($i + $j < $string_length) && $keyword[$j] == $string[$i + $j]) {
								$match++;
							}
							else {
								return false;
							}
						}
	
						if ($match == $keyword_length) {
							return true;
						}
	
						// end if first match found
					}
	
					// end if remaining part
				}
				else {
					return false;
				}
	
				// end for loop
			}
	
			// end case4
		}
	
		return false;
	}
	
}

?>